import { defineStore } from 'pinia';
import associationApi from '@/sheep/api/association.js';
import { clone } from 'lodash';

// 默认用户信息
const defaultUserInfo = {
  id: null,
  username: '',
  nickname: '',
  mobile: '',
  avatar: '',
  score: 0,
  token: '',
  user_id: null,
  createtime: '',
  expiretime: '',
  expires_in: '',
  // 是否点击置顶  点击返回我的主页重新刷新  没点击则不刷新
  stickyShowCli:false
};
// 主题设置
const theme = {
  id: 1,
  platform: 'pet',
  title_color: '#333',
  background_image:
    'https://liuliangbenben.oss-cn-hangzhou.aliyuncs.com/storage/default/20231012/73203ee772ea7e3a27903b268a5c80ef.png',
  main_color: '#f52713',
  selected_color: '#1BBDCE',
  btn_background_color: '#B5F8FF',
  likes_color: '#1BBDCE',
  collapse_color: '#1BBDCE',
  publish_image:
    'https://liuliangbenben.oss-cn-hangzhou.aliyuncs.com/storage/association/20231027/a1d5ef32aca508607a75a2424bc9230f.png',
  close_publish_image:
    'https://liuliangbenben.oss-cn-hangzhou.aliyuncs.com/storage/association/20231027/e7f9f7e9af02bbabd13a0237b3a3b68c.png',
  note_image:
    'https://liuliangbenben.oss-cn-hangzhou.aliyuncs.com/storage/association/20231113/1024a8c824aba4de5e483b7dc177eb2e.png',
  questions_image:
    'https://liuliangbenben.oss-cn-hangzhou.aliyuncs.com/storage/association/20231113/432ae75a48b84322f0a3b6cbea6e4575.png',
  visit_image:
    'https://liuliangbenben.oss-cn-hangzhou.aliyuncs.com/storage/association/20231113/62ea9386bce3218d66b7c94a72d7b318.png',
  share_image:
    'https://liuliangbenben.oss-cn-hangzhou.aliyuncs.com/storage/association/20231113/76f1b129bd298f7209ab1004c4d16f7e.png',
  createtime: null,
  updatetime: 1699925324,
};

const association = defineStore({
  id: 'association',
  state: () => ({
    userInfo: clone(defaultUserInfo), // 用户信息
    lastUpdateTime: 0, // 用户信息上次更新时间
    theme: clone(theme), //社群模块主题
    themeUpdateTime: 0, //社群主题更新时间
    examine: false, //社群审核状态
    loging: false,
  }),

  actions: {
    isLogin(){
      return !!uni.getStorageSync('associationToken') // 登录状态
    },
    // 登录
    async login() {
      if (this.loging) return;
      this.loging = true;
      const res = await associationApi.loginApi();
      this.loging = false;
     if (res) {
		 if (res.code == 0 || !res) return Promise.reject(res)
		 this.setToken(res.data.userinfo.token);
		 return Promise.resolve(res);
	 } else {
		 return Promise.resolve(false);
	 }
    },
    // 获取个人信息
    async getUserInfo() {
      const res = await associationApi.loginApi();
      if (res?.code === 0 || !res) return Promise.reject(res);
      this.userInfo = res.data.userinfo;
      return Promise.resolve(res);
    },
    // 登录后
    async loginAfter() {
      await this.updateUserData();
    },
    async upDateTheme() {
      const nowTime = new Date().getTime();
      // if (this.themeUpdateTime + 60000 > nowTime) return;
      await this.getTheme();
      this.themeUpdateTime = nowTime;
      return this.theme;
    },
    async getTheme(platform) {
      // #ifdef H5
      const { data } = await associationApi.index(platform,{ device: 'h5' });
      // #endif
      // #ifdef MP-WEIXIN
      const { data } = await associationApi.index(platform,{ device: 'miniapp' });
      // #endif

      // #ifdef APP
      const { data } = await associationApi.index(platform,{ device: 'app' });
      // #endif

      this.theme = data.setting;
      this.examine = !!(data.platform.examine_status * 1)
    },
    // 设置token
    setToken(token = '') {
      if (token === '') {
        // this.isLogin = false;
        uni.removeStorageSync('associationToken');
      } else {
        // this.isLogin = true;
        uni.setStorageSync('associationToken', token);
        this.loginAfter();
      }
      return this.isLogin();
    },

    // 更新用户相关信息 (手动限流 5秒之内不刷新)
    async updateUserData() {
      if (!this.isLogin()) {
        this.resetUserData();
        return;
      }
      const nowTime = new Date().getTime();
      if (this.lastUpdateTime + 5000 > nowTime) return;
      await this.getUserInfo();
      this.lastUpdateTime = nowTime;
      return this.userInfo;
    },

    // 重置用户默认数据
    resetUserData() {
      this.setToken();
      this.userInfo = clone(defaultUserInfo);
      this.lastUpdateTime = 0;
    },

    // 登出
    async logout() {
      this.resetUserData();
      return !this.isLogin();
    },
  },
  persist: {
    enabled: true,
    strategies: [
      {
        key: 'association-store',
        // #ifdef H5
        storage: localStorage, //存储的位置，默认为sessionStorage
        // #endif
        paths:['theme','userInfo']
      },
    ],
  },
});

export default association;
